package handler;

import handler.client.ClientDecoder;
import handler.client.ClientEncoder;
import handler.client.ClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author dfj@99chuxing.com
 * @date 2018/3/28 0028 16:32
 */
public class ClientStart {

    public static void main(String[] args) throws InterruptedException {

        // 创建客户端引导器
        Bootstrap bootstrap =new Bootstrap();
        // 创建事件循环监听器组
        NioEventLoopGroup eventLoopGroup =new NioEventLoopGroup();
        bootstrap
                .group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {

                        nioSocketChannel.pipeline()
                                .addLast("Encoder",new ClientEncoder())
                                .addLast("Decoder",new ClientDecoder())
                                .addLast(new ClientHandler());


                    }
                });

        try {

//            // 同步访问远程节点，一直阻塞到连接成功
//            ChannelFuture f = bootstrap.connect(new InetSocketAddress("192.168.0.124", 2345)).sync();
//            // 阻塞等待直到服务器的 Channel关闭
//            f.channel().closeFuture().sync();

            /** 异步地连接到远程节点 */
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("192.168.0.124", 2345));
            /** 注册一个 ChannelFutureListener，以便在操作完成时获得通知 */
            channelFuture.addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture channelFuture) throws Exception {
                                // 如果操作成功则发送数据到服务器
                                if (channelFuture.isSuccess()){
                                    System.out.println("连接成功---------------");
                                }else {
                                    Throwable cause = channelFuture.cause();
                                    cause.printStackTrace();
                                }
                    }
            });
            channelFuture.channel().closeFuture().sync();

        }finally {
            eventLoopGroup.shutdownGracefully();

        }


    }

    public void connect() throws InterruptedException {
        // 创建客户端引导器
        Bootstrap bootstrap =new Bootstrap();
        // 创建事件循环监听器组
        NioEventLoopGroup eventLoopGroup =new NioEventLoopGroup();
        bootstrap
                .group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {

                        nioSocketChannel.pipeline()
                                .addLast("Encoder",new ClientEncoder())
                                .addLast("Decoder",new ClientDecoder())
                                .addLast(new ClientHandler());


                    }
                });

        try {

//            // 同步访问远程节点，一直阻塞到连接成功
//            ChannelFuture f = bootstrap.connect(new InetSocketAddress("192.168.0.124", 2345)).sync();
//            // 阻塞等待直到服务器的 Channel关闭
//            f.channel().closeFuture().sync();
            /** 异步地连接到远程节点 */
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("192.168.0.124", 20176));
            /** 注册一个 ChannelFutureListener，以便在操作完成时获得通知 */
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    // 如果操作成功则发送数据到服务器
                    if (channelFuture.isSuccess()){
                        System.out.println("连接成功---------------");
                    }else {
                        Throwable cause = channelFuture.cause();
                        cause.printStackTrace();
                    }
                }
            });
            channelFuture.channel().closeFuture().sync();

        }finally {
            eventLoopGroup.shutdownGracefully();

        }

    }




}
